//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "CmsMarketCalibration.h"
using namespace Cephei::QL::Termstructures::Volatility::Swaption;
#include <gen/QL/Termstructures/Volatility/Swaption/CmsMarket.h>
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/Math/Optimization/EndCriteria.h>
#include <gen/QL/Math/Optimization/OptimizationMethod.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Math::Optimization;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarketCalibration::CCmsMarketCalibration (boost::shared_ptr<QuantLib::CmsMarketCalibration>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCmsMarketCalibration = NULL;
#endif
	_ppCmsMarketCalibration = &childNative;
    
}
Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarketCalibration::CCmsMarketCalibration (QuantLib::CmsMarketCalibration& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phCmsMarketCalibration = NULL;
#endif
	_ppCmsMarketCalibration = new boost::shared_ptr<QuantLib::CmsMarketCalibration> (&childNative);
    
    _CmsMarketCalibrationOwner = owner;
    
}

Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarketCalibration::CCmsMarketCalibration (CCmsMarketCalibration^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCmsMarketCalibration = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCmsMarketCalibration = new boost::shared_ptr<QuantLib::CmsMarketCalibration> (copy->GetShared());
        
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarketCalibration::CCmsMarketCalibration (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCmsMarketCalibration = NULL;
#endif
	if (!t->IsSubclassOf(CCmsMarketCalibration::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarketCalibration::CCmsMarketCalibration (QuantLib::Handle<QuantLib::CmsMarketCalibration>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCmsMarketCalibration = &childNative;
	_ppCmsMarketCalibration = &static_cast<boost::shared_ptr<QuantLib::CmsMarketCalibration>>(childNative.currentLink());
    
    _CmsMarketCalibrationOwner = owner;
}
Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarketCalibration::CCmsMarketCalibration (QuantLib::Handle<QuantLib::CmsMarketCalibration> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCmsMarketCalibration = &childNative;
	_ppCmsMarketCalibration = &static_cast<boost::shared_ptr<QuantLib::CmsMarketCalibration>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarketCalibration::CCmsMarketCalibration (QuantLib::CmsMarketCalibration childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCmsMarketCalibration = NULL;
#endif
	_ppCmsMarketCalibration = new boost::shared_ptr<QuantLib::CmsMarketCalibration> (new QuantLib::CmsMarketCalibration (childNative));
    
}
#endif

Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarketCalibration::~CCmsMarketCalibration ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppCmsMarketCalibration != NULL)
    {
	    delete _ppCmsMarketCalibration;
        _ppCmsMarketCalibration = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarketCalibration::!CCmsMarketCalibration ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppCmsMarketCalibration != NULL)
    {
	    delete _ppCmsMarketCalibration;
    }
}
QuantLib::CmsMarketCalibration& Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarketCalibration::GetReference ()
{
    if (_ppCmsMarketCalibration == NULL) throw gcnew NativeNullException ();
	return **_ppCmsMarketCalibration;
}
boost::shared_ptr<QuantLib::CmsMarketCalibration>& Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarketCalibration::GetShared ()
{
    if (_ppCmsMarketCalibration == NULL) throw gcnew NativeNullException ();
	return *_ppCmsMarketCalibration;
}
QuantLib::CmsMarketCalibration* Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarketCalibration::GetPointer ()
{
    if (_ppCmsMarketCalibration == NULL) throw gcnew NativeNullException ();
	return &**_ppCmsMarketCalibration;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CmsMarketCalibration>& Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarketCalibration::GetHandle ()
{
	if (_phCmsMarketCalibration == NULL)
	{
		_phCmsMarketCalibration = new Handle<QuantLib::CmsMarketCalibration> (*_ppCmsMarketCalibration);
	}
	return *_phCmsMarketCalibration;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarketCalibration::HasNative () 
{
	return (_ppCmsMarketCalibration != NULL);
}

Cephei::QL::Math::IArray^ Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarketCalibration::Compute (Cephei::QL::Math::Optimization::IEndCriteria^ endCriteria, Cephei::QL::Math::Optimization::IOptimizationMethod^ method, Cephei::QL::Math::IArray^ guess, Boolean isMeanReversionFixed)
{
    CEndCriteria^ _CendCriteria;
    COptimizationMethod^ _Cmethod;
    CArray^ _Cguess;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _CendCriteria = safe_cast<CEndCriteria^> (endCriteria);
        _CendCriteria->Lock();
        boost::shared_ptr<QuantLib::EndCriteria>& _endCriteria = static_cast<boost::shared_ptr<QuantLib::EndCriteria>&> (_CendCriteria->GetShared ()); 
        _Cmethod = safe_cast<COptimizationMethod^> (method);
        _Cmethod->Lock();
        boost::shared_ptr<QuantLib::OptimizationMethod>& _method = static_cast<boost::shared_ptr<QuantLib::OptimizationMethod>&> (_Cmethod->GetShared ()); 
        _Cguess = safe_cast<CArray^> (guess);
        _Cguess->Lock();
        QuantLib::Array& _guess = static_cast<QuantLib::Array&> (_Cguess->GetReference ()); 
        bool _isMeanReversionFixed = (bool)ValueHelper::Convert (isMeanReversionFixed);
    	QuantLib::Array _rv = (QuantLib::Array)(*_ppCmsMarketCalibration)->compute ( _endCriteria,  _method,  _guess,  _isMeanReversionFixed );   
        Cephei::QL::Math::CArray^ _nrv = gcnew Cephei::QL::Math::CArray (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CendCriteria != nullptr) _CendCriteria->Unlock();
        if (_Cmethod != nullptr) _Cmethod->Unlock();
        if (_Cguess != nullptr) _Cguess->Unlock();
    }
}
QL::Math::Optimization::EndCriteria::TypeEnum Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarketCalibration::EndCriteria::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::EndCriteria::Type _rv = (QuantLib::EndCriteria::Type)(*_ppCmsMarketCalibration)->endCriteria ( );   
        QL::Math::Optimization::EndCriteria::TypeEnum _nrv = (QL::Math::Optimization::EndCriteria::TypeEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Swaption::CCmsMarketCalibration::Error::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCmsMarketCalibration)->error ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

